⚡️ Speed up method DocumentUrl._infer_media_type
by 12% in PR #35 (trigger-cf-workflow
)
#36
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #35
If you approve this dependent PR, these changes will be merged into the original PR branch
trigger-cf-workflow
.📄 12% (0.12x) speedup for
DocumentUrl._infer_media_type
inpydantic_ai_slim/pydantic_ai/messages.py
⏱️ Runtime :
23.8 milliseconds
→21.3 milliseconds
(best of30
runs)📝 Explanation and details
Here is an optimized version of your Python program. Major optimizations.
guess_type
per unique URL usingfunctools.lru_cache
, which reduces repeated MIME type computations (especially on large scale repeated calls).FileUrl
, it is best to avoid repeating the dataclass and repr decorators if already present in the parent (maintaining runtime correctness and consistency).Notes.
_guess_type_cached
helper is a staticmethod, so it's shared across all instances and efficiently caches guess_type results.maxsize=None
to cache unlimited.dataclass
andrepr
decorators are not required here becauseFileUrl
already establishes the base data model and behaviors for you.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr35-2025-07-25T03.11.24
and push.